package cn.lvhang.org.service.impl;

import cn.lvhang.basic.service.impl.BaseServiceImpl;
import cn.lvhang.basic.util.PageList;
import cn.lvhang.org.domain.Department;
import cn.lvhang.org.mapper.DepartmentMapper;
import cn.lvhang.org.query.DepartmentQuery;
import cn.lvhang.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)//不支持事务
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional  // 支持事务  (readOnly = false, propagation = Propagation.REQUIRED)
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }
    @Override
    @Transactional
    public void update(Department department) {
        //封装dirPath
        String dirPath = "";
        //判断是否添加顶级部门
        //是顶级部门
        if(department.getParent() == null){
            dirPath = "/"+department.getId();//直接拼接“/”加新增的ID
        }else{ //非顶级部门
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            dirPath = parentDept.getDirPath() + "/"+department.getId();//在前面拼接父部门的ID
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }
    @Override
    public List<Department> loadTree() {
        return departmentMapper.loadTree();
    }
}
